package com.internship.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.internship.model.Bjdmb;
import com.internship.model.DepartmentInfo;
import com.internship.model.ProfessionInfo;
import com.internship.model.StudentInfo;
import com.internship.page.Page;
import com.internship.service.BjdmbService;
import com.internship.service.Const;
import com.internship.service.DepartmentInfoService;
import com.internship.service.ProfessionInfoService;
import com.internship.service.StudentInfoService;
import com.internship.util.DesTool;
import com.internship.util.StringUtil;


@Controller
@RequestMapping("student")
public class StudentController {
	private final Logger logger = Logger.getLogger(StudentController.class);
	
	@Autowired
	private ProfessionInfoService professionInfoService;
	@Autowired
	private DepartmentInfoService 	departmentInfoService;
	@Autowired
	private BjdmbService 	bjdmbService;
	@Autowired
	private StudentInfoService 	studentInfoService;
	public static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attrs.getRequest();
	}
	private void clearSession() {
		getSession().invalidate();
	}
	private void saveSessionStu(StudentInfo studentInfo) {
		HttpSession session = getSession();
		session.setAttribute(Const.SESSION_ACCOUNT, studentInfo.getXH());
		session.setAttribute(Const.USER_NAME, studentInfo.getXM());
		session.setAttribute(Const.GRADE, studentInfo.getDQSZJ());
		session.setAttribute(Const.USER_PHONE, studentInfo.getLXDH());
		session.setAttribute(Const.USER_SEX, studentInfo.getXB());
		session.setAttribute(Const.USER_DEPARTMENT_NAME, studentInfo.getXY());
		session.setAttribute(Const.USER_CLASSINFO, studentInfo.getXZB());
		session.setAttribute(Const.USER_ROLE, Const.ROLE_STUDENT);
		session.setAttribute(Const.USER_ROLE_NAME, Const.ROLE_STUDENT_NAME);
	}
	@RequestMapping("/studentInfo_list.do")
	public ModelAndView getStudentInfoList() {
		return getStudentInfoList(null,null,null,null,0,1);
	}
	private List<DepartmentInfo>  departmentInfoList = null;
	private List<ProfessionInfo>  professionInfoList = null;
	private List<Bjdmb>  bjdmbList = null;
	@RequestMapping("/studentInfo_page_list.do")
	public ModelAndView getStudentInfoList(@RequestParam String XM,@RequestParam String XY,@RequestParam String ZYDM,@RequestParam String XZB,@RequestParam int DQSZJ,@RequestParam int pageNumber) {
		ModelAndView mv = new ModelAndView("student_info");
		HttpSession session = getSession();
		try {
			if (String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("2")||String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("1")) {
				Page<StudentInfo> pageList = studentInfoService.getStudentInfoList(XM, XY, ZYDM, XZB, DQSZJ, pageNumber);
				this.departmentInfoList = departmentInfoService.getDepartmentInfoList();
				if(XY==null||XY.equals("")){
					this.professionInfoList = professionInfoService.getProfessionInfoList();
					this.bjdmbList = bjdmbService.getBjdmbList();
				}else {
					DepartmentInfo departmentInfo=	departmentInfoService.getDepartmentInfo(XY);
					if (departmentInfo!=null) {
						this.professionInfoList = professionInfoService.getProfessionInfo(departmentInfo.getXYDM());
						if(ZYDM!=null){
							this.bjdmbList = bjdmbService.getBjdmb(departmentInfo.getXYDM(),ZYDM);
						}else {
							this.bjdmbList = bjdmbService.getBjdmb(departmentInfo.getXYDM(),null);	
						}
					}
				}
				mv.addObject("currentPage", pageNumber);
				mv.addObject("XY", XY);
				mv.addObject("ZYDM", ZYDM);
				mv.addObject("XZB", XZB);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("departmentInfoList",departmentInfoList);
				mv.addObject("professionInfoList",professionInfoList);
				mv.addObject("bjdmbList",bjdmbList);
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("studentInfoList", pageList.getResultList());
			}else {
				this.professionInfoList = professionInfoService.getProfessionInfo(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)));
				this.bjdmbList = bjdmbService.getBjdmb(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)),null);
				Page<StudentInfo> pageList = studentInfoService.getStudentInfoList(XM, String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_NAME)), ZYDM, XZB, DQSZJ, pageNumber);
				mv.addObject("currentPage", pageNumber);
				mv.addObject("bjdmbList",bjdmbList);
				mv.addObject("professionInfoList",professionInfoList);
				mv.addObject("ZYDM", ZYDM);
				mv.addObject("XZB", XZB);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("studentInfoList", pageList.getResultList());
			}
			String[] gradeList = StringUtil.getCurrentGrades();
			mv.addObject("gradeList", gradeList);
			mv.addObject("DQSZJ", DQSZJ);
			mv.addObject("XM", XM);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return mv;
	}
	@RequestMapping("/departmentInfo.do")
	@ResponseBody
	public JSONObject departmentInfo(@RequestParam("XYMC")String  XYMC) {
		JSONObject json = new JSONObject();
		DepartmentInfo departmentInfo=	departmentInfoService.getDepartmentInfo(XYMC);
		if (departmentInfo!=null) {
			this.professionInfoList = professionInfoService.getProfessionInfo(departmentInfo.getXYDM());
			this.bjdmbList = bjdmbService.getBjdmb(departmentInfo.getXYDM(),null);
		}else {
			this.professionInfoList = professionInfoService.getProfessionInfoList();
			this.bjdmbList = bjdmbService.getBjdmbList();
		}
		json.put("bjdmbList", bjdmbList);
		json.put("professionInfoList", professionInfoList);
		return json;
	}
	@RequestMapping("/professionInfo.do")
	@ResponseBody
	public JSONObject professionInfo(@RequestParam("ZYDM")String  ZYDM,@RequestParam("XYMC")String  XYMC) {
		JSONObject json = new JSONObject();
		DepartmentInfo departmentInfo=	departmentInfoService.getDepartmentInfo(XYMC);
		if (departmentInfo!=null) {
		this.bjdmbList = bjdmbService.getBjdmb(departmentInfo.getXYDM(),ZYDM);
	
		}else {
			this.bjdmbList = bjdmbService.getBjdmb(null,ZYDM);
		}	
		json.put("bjdmbList", bjdmbList);
		return json;
	}
	/**
	 * 手机登陆页面
	 * @return
	 */
	@RequestMapping("/m_login.do")
	public ModelAndView mlogin() {
		ModelAndView mv = new ModelAndView("login_moible");
		return mv;
	}
	@RequestMapping("/login.do")
	@ResponseBody
    public Object userLogin(@RequestParam String KEYDATA) {
		logger.info("执行userLogin方法");
		HttpSession session = getSession();
		String errInfo = "";
		JSONObject json = new JSONObject();
		String KEY[] = KEYDATA.split(",zt,");
		 StudentInfo studentInfo = studentInfoService.getStudentInfoByXH(KEY[0]);
		if(studentInfo != null && DesTool.decode(studentInfo.getPwd()).equals(KEY[1])) {
		 DepartmentInfo departmentInfo=   departmentInfoService.getDepartmentInfo(studentInfo.getXY());
		 session.setAttribute(Const.USER_DEPARTMENT_CODE, departmentInfo.getXYDM());
		 saveSessionStu(studentInfo);
			errInfo = "success";
			logger.info("------userLogin  userLogin = " + KEY[0]);
		}else {
			errInfo = "usererror";		
			}
		json.put("result", errInfo);
		return json;
	}
	@RequestMapping("/index.do")
    public ModelAndView indexPage() {
		logger.info("执行indexPage方法");
		ModelAndView mv = new ModelAndView();
		mv.setViewName("moible_main");
		return mv;
	}
	@RequestMapping("/home.do")
    public ModelAndView home() {
		logger.info("执行home方法");
		ModelAndView mv = new ModelAndView();
		mv.setViewName("home_page");
		return mv;
	}
}